package com.qianfeng.service.impl;

import com.qianfeng.dao.PatientDao;
import com.qianfeng.entity.Patient;
import com.qianfeng.entity.PatientSta;
import com.qianfeng.entity.ProSta;
import com.qianfeng.entity.User;
import com.qianfeng.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PatientServiceImpl implements PatientService {

    @Resource
    PatientDao patientDao;

    @Autowired
    private HttpSession session;

    /**
     * 读取ECahrt绘图数据（中国）
     * @return
     */
    @Override
    public List<ProSta> queryProSta() {
        return patientDao.queryProSta();
    }

    @Override
    public List<ProSta> queryProStaByCity(String city) {
        return patientDao.queryProStaByCity(city);
    }

    @Override
    public List<ProSta> queryProStaByProvince(String province) {
        return patientDao.queryProStaByProvince(province);
    }

    @Override
    public List<Patient> queryPatient(String city) {
        return patientDao.queryCityPatient(city);
    }

    @Override
    public List<Patient> queryProvincePatient(String province) {
        return patientDao.queryProvincePatient(province);
    }

    @Override
    public boolean addPatient(Patient patient) {
//        User user = (User) session.getAttribute("USER");
        User user = new User();
        user.setId(110);
        user.setUsername("zhang");
        System.out.println("patient = " + patient);
        //注入Session中的id ?????
        patient.setCreateDatetime(new Date());

        patient.setCreateUserId(user.getId());
        return patientDao.addPatient(patient) > 0 ? true : false;
    }

    @Override
    public boolean updatePatient(Patient patient) {
        //        User user = (User) session.getAttribute("USER");
        User user = new User();
        user.setId(110);
        user.setUsername("zhang");
        System.out.println("patient = " + patient);
        //注入Session中的id ?????

        return patientDao.updatePatient(patient) > 0 ? true : false;
    }

    @Override
    public boolean deletePatient(Integer patientId) {
        return patientDao.deletePatient(patientId) > 0 ? true : false;
    }

    /**
     * 根据城市查询数量
     * @param
     * @return
     */
    public Map<String, Integer> getCount(String city) {
        Map<String, Integer> map = new HashMap<>();

        //获取累计数量
        Integer count = patientDao.getCityCount(city);
        map.put("count", count);
        //获取昨日新增确诊
        Integer beforeCount = patientDao.getCitySureYesterday(city);
        map.put("beforeCount", beforeCount);

        //累计治愈
        Integer cureCount = patientDao.getCityCountBysta(city, "1");  //gai
        map.put("cureCount", cureCount);
        //昨日累计治愈
        Integer beforeCure = patientDao.getCityCureYesterday(city);
        map.put("beforeCure", beforeCure);

        //死亡人数
        Integer dieCount = patientDao.getCityCountBysta(city, "4");
        map.put("dieCount", dieCount);
        //昨日死亡累计
        Integer beforeDieCount = patientDao.getCityDieYesterday(city);
        map.put("beforeDieCount", beforeDieCount);

        //现有确诊
        Integer now = count - cureCount - dieCount;
        map.put("now", now);
        //昨日确诊
        Integer before = patientDao.getCitySureYesterday(city);
        map.put("before", before);




        return map;
    }

    @Override
    public Map<String, Integer> getProvinceCount(String province) {
        Map<String, Integer> map = new HashMap<>();

        //获取累计数量
        Integer count = patientDao.getProvinceCount(province);
        map.put("count", count);
        //获取昨日新增确诊
        Integer beforeCount = patientDao.getProvinceSureYesterday(province);
        map.put("beforeCount", beforeCount);

        //累计治愈
        Integer cureCount = patientDao.getProvinceCountBysta(province, "1");  //gai
        map.put("cureCount", cureCount);
        //昨日累计治愈
        Integer beforeCure = patientDao.getProvinceCureYesterday(province);
        map.put("beforeCure", beforeCure);

        //死亡人数
        Integer dieCount = patientDao.getProvinceCountBysta(province, "4");
        map.put("dieCount", dieCount);
        //昨日死亡累计
        Integer beforeDieCount = patientDao.getProvinceDieYesterday(province);
        map.put("beforeDieCount", beforeDieCount);

        //现有确诊
        Integer now = count - cureCount - dieCount;
        map.put("now", now);
        //昨日确诊
        Integer before = patientDao.getProvinceSureYesterday(province);
        map.put("before", before);




        return map;
    }

    @Override
    public Patient getPatientById(Integer id) { //my
        Patient patient = new Patient();
        patient.setPatientName("wang");
        return patient;
//        return patientDao.queryOneById(id);
    }

}
